<template>
	<goods-sku-popup :value="visible" theme='green' @input="onChangeValue" border-radius="20" :localdata="goodsInfo" :mode="skuMode"
		:maskCloseAble="true"  @open="openSkuPopup" @close="closeSkuPopup" @add-cart="addCart" @buy-now="buyNow"
		buyNowText="立即购买" />
</template>

<script>
	const app = getApp();
	import GoodsSkuPopup from '@/components/goods-sku-popup'

	export default {
		name:'sku-modal',
		components: {
			GoodsSkuPopup
		},
		model: {
			prop: 'value',
			event: 'input'
		},
		props: {
			// true 组件显示 false 组件隐藏
			// value: {
			// 	Type: Boolean,
			// 	default: false
			// },
			// 模式 1:都显示 2:只显示购物车 3:只显示立即购买
			skuMode: {
				type: Number,
				default: 1
			},
			// 商品详情信息
			goods: {
				type: Object,
				default: {}
			},
		},
		watch:{
			goods(newVal,oldVal){
				this.goods = newVal;
				console.log('this.goods 2222',this.goods)
				const that = this
				const {
					goods
				} = that
				that.goodsInfo = {
					_id: goods.goods_id,
					name: goods.goods_name,
					goods_thumb: goods.goods_image,
					sku_list: that.getSkuList(),
					spec_list: that.getSpecList(),
					spec_type:goods.spec_type
				}
				console.log('goodsInfo',that.goodsInfo)
			}
		},

		data() {
			return {
				// 是否显示
				visible: false,
				goodsInfo: {},
			}
		},

		created() {
			
		},

		methods: {
			toShowSkuPopup(){
				this.visible = true;
			},
			// 监听组件显示隐藏
			onChangeValue(val) {
				console.log('val',val)
				this.visible = val
			},

			// 整理商品SKU列表
			getSkuList() {
				const that = this
				const {
					goods: {
						goods_name,
						goods_image,
						skuList
					}
				} = that
				const skuData = []
				skuList.forEach(item => {
					skuData.push({
						_id: item.id,
						goods_sku_id: item.goods_sku_id,
						goods_id: item.goods_id,
						goods_name: goods_name,
						image: item.image ? item.image : goods_image,
						price: item.goods_price * 100,
						stock: item.stock_num,
						unit:item.goods_unit,
						minimum:item.minimum,
						spec_value_ids: item.spec_value_ids,
						sku_name_arr: that.getSkuNameArr(item.spec_value_ids)
					})
				})
				return skuData
			},

			// 获取sku记录的规格值列表
			getSkuNameArr(specValueIds) {
				const that = this
				const defaultData = ['默认']
				const skuNameArr = []
				if (specValueIds) {
					specValueIds.forEach((valueId, groupIndex) => {
						const specValueName = that.getSpecValueName(valueId, groupIndex)
						skuNameArr.push(specValueName)
					})
				}
				return skuNameArr.length ? skuNameArr : defaultData
			},

			// 获取指定的规格值名称
			getSpecValueName(valueId, groupIndex) {
				const that = this
				const {
					goods: {
						specList
					}
				} = that
				const res = specList[groupIndex].valueList.find(specValue => {
					return specValue.spec_value_id == valueId
				})
				return res.spec_value
			},

			// 整理规格数据
			getSpecList() {
				const {
					goods: {
						specList
					}
				} = this
				const defaultData = [{
					name: '默认',
					list: [{
						name: '默认'
					}]
				}]
				
				if(this.goods.spec_type == 10){
					return defaultData;
				}else{
					const specData = []
					specList.forEach(group => {
						const children = []
						group.valueList.forEach(specValue => {
							children.push({
								name: specValue.spec_value
							})
						})
						specData.push({
							name: group.spec_name,
							list: children
						})
					})
					return specData.length ? specData : defaultData
				}
			},

			// sku组件 开始-----------------------------------------------------------
			openSkuPopup() {
				// console.log("监听 - 打开sku组件")
			},

			closeSkuPopup() {
				// console.log("监听 - 关闭sku组件")
			},

			// 加入购物车按钮
			addCart(selectShop) {
				console.log('selectShop',selectShop)
				const that = this;
				app.globalRequest({
					api:app.getApi().goods.addCart,
					method:'POST',
					data:{
						goodsId:selectShop.goods_id,
						goodNum:selectShop.buy_num,
						goodsSkuId:selectShop.goods_sku_id,
					}
				}).then(res=>{
					if(res.code == 0){
						uni.showToast({
							title:res.msg,
							icon:'success',
							duration:2000
						})
						setTimeout(()=>{
							that.visible = false;
						},2000)
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none',
							duration:2000
						})
						if(res.code == 10024){
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/user-page/enterprise-auth/enterprise-auth?is_auth=0'
								})
							},2000)
						}
					}
				})
			},

			// 立即购买
			buyNow(selectShop) {
				console.log('selectShop',selectShop)
				const that = this;
				that.onChangeValue(false);
				uni.navigateTo({
					url:`/pages/goods-page/submit-order/submit-order?mode=buyNow&goodsId=${selectShop.goods_id}&goodsSkuId=${selectShop.goods_sku_id}&goodsNum=${selectShop.buy_num}`
				})
			}

		}
	}
</script>

<style lang="scss" scoped>

</style>
